package cn.sfa.service.log;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;

import cn.sfa.common.pagination.Pagination;
import cn.sfa.common.utils.CommonUtil;
import cn.sfa.common.utils.MajorKeyGenerate;
import cn.sfa.common.utils.TableNameEnum;
import cn.sfa.entity.log.SaleJobLogs;
@Service
public class LogService {

	Logger logger = LoggerFactory.getLogger(LogService.class);
	
	@Autowired
	NamedParameterJdbcTemplate jdbcTemplate;
	
	public NamedParameterJdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(NamedParameterJdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Autowired
	MajorKeyGenerate majorKeyGenerate;
	
	/**
	 * 记录日志信息 
	 * @param jobLogs  日志实体类
	 * @return
	 */
	public boolean InsertToJobLog(SaleJobLogs jobLogs)
	{
		boolean returnValue = false;
		
		String sql = "insert into SALEJOBLOGS (salejoblogsid,jobname,description,starttime,endtime,secondruntime,createdate,createuid,updatedate,updateuid,remark,ex1,ex2,ex3)" +
				" values(:salejoblogsid,:jobname,:description,:starttime,:endtime,:secondruntime,:createdate,:createuid,:updatedate,:updateuid,:remark,:ex1,:ex2,:ex3)";
		
		Map<String,Object> paramMap = new HashMap<String,Object>();
		
		paramMap.put("salejoblogsid", majorKeyGenerate.keyGenerate(TableNameEnum.SALEJOBLOGS));
		
		paramMap.put("jobname", jobLogs.getJobName());
		
		paramMap.put("description", jobLogs.getDescription());
		
		paramMap.put("starttime", jobLogs.getStartTime());
		
		paramMap.put("endtime", jobLogs.getEndTime());
		
		paramMap.put("secondruntime", jobLogs.getSecondRunTime());
		
		paramMap.put("createdate", jobLogs.getCreateDate());
		
		paramMap.put("createuid", jobLogs.getCreateUid());
		
		paramMap.put("updatedate", jobLogs.getUpdateDate());
		
		paramMap.put("updateuid", jobLogs.getUpdateId());
		
		paramMap.put("remark", jobLogs.getRemark());
		
		paramMap.put("ex1", jobLogs.getVcol1());
		
		paramMap.put("ex2", jobLogs.getVcol2());
		
		paramMap.put("ex3", jobLogs.getVcol3());
		
		int count = jdbcTemplate.update(sql, paramMap);
		
		if(count>0) returnValue = true;
		
		return returnValue;
	}
	
	/**
	 * 根据日期查job日志
	 * TODO
	 * @author: LiXiang
	 * @param currentPage
	 * @param numPerPage
	 * @param url
	 * @param fromDate
	 * @param toDate
	 * @return
	 */
	public Pagination getLogsByDate(int currentPage, int numPerPage, String url, String fromDate,String toDate){
		String sql = "select * from table(pkg_sfa_logs.getJobLogsByDate(:fromDate,:toDate))";
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("fromDate", CommonUtil.jugeAndFixNull(fromDate));
		paramMap.put("toDate", CommonUtil.jugeAndFixNull(toDate));
		
		return new Pagination(sql, paramMap, currentPage, numPerPage, url,
				jdbcTemplate);
	}
	
	/**
	 * 详细
	 * TODO
	 * @author: LiXiang
	 * @param currentPage
	 * @param numPerPage
	 * @param url
	 * @param date
	 * @return
	 */
	public Pagination getLogsDetail(int currentPage, int numPerPage, String url, String date){
		String sql = "select * from table(pkg_sfa_logs.getJobLogsDetail(:date))";
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("date", CommonUtil.jugeAndFixNull(date));
		
		return new Pagination(sql, paramMap, currentPage, numPerPage, url,
				jdbcTemplate);
	}
}
